package com.shk.service.imp;


import com.alibaba.dubbo.config.annotation.Service;
import com.shk.dao.PermissionDao;
import com.shk.dao.RoleDao;
import com.shk.dao.UserDao;
import com.shk.pojo.Permission;
import com.shk.pojo.Role;
import com.shk.pojo.User;
import com.shk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public User findByUsername(String username) {
        /*通过姓名拿到用户信息*/
       User user= userDao.findByUserName(username);
       if(user==null){
           return null;
       }
        Integer userId = user.getId();
        /*通过user  id 拿到角色信息*/
       Set<Role> roles= roleDao.findByUserId(userId);
       if(roles!=null && roles.size()>0){
           for(Role role:roles){
               Integer roleId = role.getId();
               Set<Permission> permissions = permissionDao.findByRoleId(roleId);
               role.setPermissions(permissions);
           }
           user.setRoles(roles);
       }

        return user;

    }
}
